/*
 *  Copyright 2012 Yuriy Dunko.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.pc.services;

import java.util.ArrayList;
import java.util.List;

import org.opu.pc.domain.PublisherManagement;
import org.opu.pc.domain.domains.Publisher;
import org.opu.pc.services.beans.PublicationBean;
import org.opu.pc.services.beans.PublisherBean;

/**
 *
 * @author Yuriy Dunko
 */
public class PublisherServiceImpl implements PublisherService {

    private PublisherManagement publisherManagement;

    public PublisherServiceImpl(PublisherManagement publisherManagement) {
        this.publisherManagement = publisherManagement;
    }

    @Override
    public List<PublisherBean> list() {
        return toBeans(publisherManagement.list());
    }

    @Override
    public List<PublisherBean> searchByName(String name, String secondName,
            String lastName) {
        return toBeans(publisherManagement.searchByName(name, secondName,
                lastName));
    }
    
    @Override
    public List<PublisherBean> searchPublishersByStudentBelonging(Boolean isSruden) {
    	return toBeans(publisherManagement.searchPublishersByStudentBelonging(isSruden));
    }

    @Override
    public PublisherBean getByName(String name, String secondName,
            String lastName) {
        return toBean(publisherManagement.getByName(name, secondName,  lastName));
    }

    @Override
    public List<PublisherBean> publishersForPublication(PublicationBean publication) {
        return toBeans(publisherManagement.publishersForPublication(
                publication.getId()));
    }

    @Override
    public PublisherBean createNew(String name, String secondName, 
    		String lastName, boolean isStudent) {
        return toBean(publisherManagement.createNew(name, secondName, lastName,
                isStudent));
    }

    @Override
    public void deletePublisher(Integer id){
        publisherManagement.deletePublisher(id);
    }

    @Override
    public void updatePublisher(PublisherBean bean){
        Publisher publisher = new Publisher(bean.getId(), bean.getName(), 
                bean.getSecondName(), bean.getLastName());
        publisher.setIsStudent(bean.isStudent());
        publisherManagement.updatePublisher(publisher);
    }

    @Override
    public void merge(PublisherBean etalon, List<PublisherBean> duplicators) {
        List<Integer> list = new ArrayList<Integer>(duplicators.size());
        for (PublisherBean publisherBean : duplicators) {
            list.add(publisherBean.getId());
        }
        publisherManagement.merge(etalon.getId(), list);
    }

    private PublisherBean toBean(Publisher p) {
        return new PublisherBean(p.getId(), p.getName(),
                p.getSecondName(), p.getLastName(), p.getIsStudent());
    }

    private List<PublisherBean> toBeans(List<Publisher> list) {
        List<PublisherBean> ret = new ArrayList<PublisherBean>(list.size());
        for (Publisher p : list) {
            ret.add(toBean(p));
        }
        return ret;
    }

}
